﻿using AntdUI;
using Lima.ResumeSys.DAL;
using Lima.ResumeSys.Models;
using Lima.ResumeSys.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace Lima.ResumeSys
{
    public partial class PersonInfoControl : UserControl
    {
        private AntdUI.Window window;
        private QueryPersonInfoModel query = new QueryPersonInfoModel();
        /// <summary>
        /// 当前组织节点ID
        /// </summary>
        private string CurrentOrgId=string.Empty;
        PerSonInfoDAL dal = new PerSonInfoDAL();
        OrganizationDAL orgdal = new OrganizationDAL();
        public PersonInfoControl(AntdUI.Window _window)
        {
            window = _window;
            InitializeComponent();
            BindTreeViewData();//绑定左侧树形结构
            InitTableColumns();//初始化TABLE列
            InitData();//初始化用户信息
        }
        private void InitTableColumns()
        {
            usertable.Columns = new ColumnCollection() {
                new Column("","序号"){
                    Width = "50",
                    Render = (value,record,rowindex)=>{return (rowindex+1); },
                    Fixed = true,//冻结列
                },
                new Column("PerName", "姓名", ColumnAlign.Left),
                new Column("PerSex", "性别", ColumnAlign.Center),
                new Column("PerBirthText", "出生年月",ColumnAlign.Center),
                new Column("PerTenureTimeText", "任职时间",ColumnAlign.Center),
                new Column("PerJobPosition", "职务",ColumnAlign.Left),
                new Column("PerJobLevel", "级别",ColumnAlign.Center),
                new Column("PerJobTitle", "职称"),
                new Column("PerManagementWork", "分管工作"),
                new Column("CellLinks", "操作", ColumnAlign.Center),
            };

        }

        private void BindTableData(PersonPageResult result)
        {
            //antList = new List<PersonInfo>();

            foreach (var item in result.Data)
            {
                item.CellLinks= new CellLink[] {
                    new CellButton(item.PerNo,"编辑",TTypeMini.Primary),
                    new CellButton(item.PerNo,"删除",TTypeMini.Error),
                };
            }
            usertable.DataSource = result.Data;

            userPagination.Total = result.Total;
            userPagination.Current = result.CurrentPage;
            userPagination.PageSize = result.PageSize;
            query.CurrentPage = result.CurrentPage;
            query.PageSize = result.PageSize;
        }

        private void InitData()
        {
            //if (string.IsNullOrEmpty(this.CurrentOrgId))
            //{
            //    MessageBox.Show("请选择组织节点!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return;
            //}
            query.PerIdCard = txtIdCard.Text;
            query.PerName = txtName.Text;
            query.PerOrgId = this.CurrentOrgId;
            var result = dal.QueryPage(query);
            BindTableData(result);
        }

        /// <summary>
        /// 绑定组织数据树
        /// </summary>
        private void BindTreeViewData()
        {
            var dataTable = orgdal.GetTreeViewData();

            var allData = dataTable.AsEnumerable().Select(row => new Organization
            {
                ID = row["Id"].ToString(),
                Code = row["Code"].ToString(),
                OrgName = row["OrgName"].ToString(),
                ParCode = row["ParCode"].ToString(),
            }).ToList();

            TreeBuilder treeBuilder = new TreeBuilder();

            treeBuilder.BindTreeViewWithParentId(orgTree, allData,CurrentUser.UserOrgId);

            orgTree.ExpandAll();
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            PersonInfoForm personInfoForm = new PersonInfoForm("", this.CurrentOrgId);
            personInfoForm.ShowDialog();
            if (personInfoForm.DialogResult == DialogResult.OK)
            {
                InitData();
            }
        }

        private void userPagination_ValueChanged(object sender, PagePageEventArgs e)
        {
           
            query.CurrentPage = e.Current;
            InitData();
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button21_Click(object sender, EventArgs e)
        {
            InitData();
        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtIdCard.Text = "";
            query.CurrentPage = 1;
            this.CurrentOrgId = "";
            InitData();
        }

        private void usertable_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            switch (e.Btn.Text)
            {
                //暂不支持进入整行编辑，只支持指定单元格编辑，推荐使用弹窗或抽屉编辑整行数据
                case "编辑":
                    PersonInfoForm form = new PersonInfoForm(e.Btn.Id,"");
                    form.ShowDialog();
                    if (form.DialogResult == DialogResult.OK)
                    {
                        InitData();
                    }
                    break;
                case "删除":
                    var result = Modal.open(window, "删除警告！", "确认要删除选择的数据吗？", TType.Warn);
                    if (result == DialogResult.OK)
                    {
                        if (dal.DelPerSonInfo(e.Btn.Id))
                        {
                            AntdUI.Message.success(window, "删除成功！", autoClose: 3);
                        }
                        else
                        {
                            AntdUI.Message.error(window, "删除失败！", autoClose: 3);
                        }
                    }
                    break;
            }

        }

        /// <summary>
        /// 点击节点显示对应的子节点数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void orgTree_NodeMouseClick(object sender, TreeSelectEventArgs e)
        {
            this.CurrentOrgId = e.Item.ID.ToString();
            query.PerOrgId = this.CurrentOrgId;
            InitData();
        }
    }
}
